import { createRouter, createWebHashHistory } from 'vue-router'
const routes = [
  {
    path: '/',
    name: 'layout',
    component: () => import('../views/layout/index.vue'),
    redirect: 'home',
    children: [
      {
        path: 'home',
        name: 'home',
        component: () => import('../views/home/index.vue'),
        meta: {
          keepAlive: true,
        },
      },
      {
        path: 'register',
        name: 'register',
        component: () => import('../views/register/index.vue'),
        meta: {
          keepAlive: false,
        },
      },
      {
        path: 'complaints',
        name: 'complaints',
        component: () => import('../views/complaints/index.vue'),
        meta: {
          keepAlive: false,
        },
      },
      {
        path: 'error',
        name: 'error',
        component: () => import('../views/error/index.vue'),
        meta: {
          keepAlive: false,
        },
      },
    ],
  },
]

export const router = createRouter({
  // 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
  history: createWebHashHistory(),
  routes, // `routes: routes` 的缩写
})

router.beforeEach((to, from, next) => {
  if (to.query.id) {
    localStorage.setItem('id', to.query.id)
    next()
  } else {
    // localStorage.setItem('id', 784780)
    // localStorage.setItem('id', 802812)
    next()

  }
  // console.log(from);
  // console.log(to);
  // if (to.path == '/complaints' || to.path == '/register') {
  //   next()
  // }
  // if (from.path == '/' && to.query.id) {
  //   localStorage.setItem('id', to.query.id)
  //   next()
  // } else {
  //   next()
  // }
  // else {
  //   router.push('/error')
  // }
  // if (to.path != '/login' && !Local.get('token')) {
  //   router.push('/login')
  // } else {
  //   next()
  // }
})
